package com.savitar.role.service;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.savitar.common.annotation.Create;
import com.savitar.common.annotation.Update;
import com.savitar.role.dao.RoleMapper;
import com.savitar.role.entity.Role;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * (Role)服务接口
 *
 * @author Savitar
 * @since 2021-05-15 23:20:56
 */
@Service
public class RoleService extends ServiceImpl<RoleMapper, Role> {


    public Page<Role> page(Integer pageNum, Integer pageSize, String name) {
        Page<Role> rolePage = new Page<>(pageNum, pageSize);
        return lambdaQuery().eq(StringUtils.isNotBlank(name), Role::getName, name)
                .or().eq(StringUtils.isNotBlank(name), Role::getNoteDesc, name)
                .orderByAsc(Role::getOrderNum).page(rolePage);
    }

    @Create
    public Role create(Role createVo) {
        createVo.setPkId(IdUtil.simpleUUID());
        createVo.insert();
        return createVo;
    }

    @Update
    public Role update(Role updateVo) {
        updateVo.updateById();
        return updateVo;
    }

    public Role deleteById(String id) {
        Role role = new Role();
        role.setPkId(id);
        role = role.selectById();
        role.deleteById();
        return role;
    }

}